package com.zlzxm.shoesmarket.viewmodel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.zlzxm.shoesmarket.net.BaseCallBack;
import com.zlzxm.shoesmarket.net.repository.NoteRepository;
import com.zlzxm.shoesmarket.net.respone.BaseResponse;
import com.zlzxm.shoesmarket.net.respone.RpNoteList;
import com.zlzxm.shoesmarket.util.RxAndroidHelp;
import com.zlzxm.shoesmarket.view.adapter.entity.EntityNote;
import com.zlzxm.shoesmarket.view.adapter.entity.SectionNote;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Consumer;

/**
 * CREATE BY zlzxm ON 4/21/21
 */
public class VMNote extends BaseViewModel {

    NoteRepository noteRepository = new NoteRepository();

    public MutableLiveData<List<SectionNote>> liveNote = new MutableLiveData<>();

    public MutableLiveData<List<String>> liveMonth = new MutableLiveData<>();

    public MutableLiveData<String> liveSelectedMonth = new MutableLiveData<>(new SimpleDateFormat("MM").format(new Date()));



    private int pageNum = 1;

    private int pagesize = 10;


    public void requestData(){

        String month = liveSelectedMonth.getValue();
         month = "04-02 22:12:2";

        noteRepository.list("2021-"+month,pageNum+"",pagesize+"")
                .compose(RxAndroidHelp.turn())
                .subscribe(new BaseCallBack<RpNoteList>() {
                    @Override
                    protected void succeed(String msg, RpNoteList rpNoteList) {

                        liveNote.setValue(createData(rpNoteList));
                    }

                    @Override
                    protected void failed(String msg) {

                        liveErrorToast.setValue(msg);
                    }

                    @Override
                    protected void notLogin() {

                    }
                },throwableCallback);



    }

    public void requestMonth(){

        List<String> strings = new ArrayList<>();

        for (int i = 1; i < 13; i++) {


            strings.add(String.format("%02d",i));


        }

        liveMonth.setValue(strings);


    }

    //<editor-fold defaultState="collapsed" desc="测试">

    public List<SectionNote> createData(RpNoteList source){

        List<SectionNote> list = new ArrayList<>();

        String head = "";

        for (int i = 0; i < source.getList().size() ;i++){

            RpNoteList.EntityList entityList = source.getList().get(i);

            if(!entityList.getBillTime().equals(head)) {

                SectionNote  sectionNote = new SectionNote(true);

                sectionNote.setDesc(entityList.getBillTime());
                sectionNote.setHead(entityList.getBillTime());
                head = entityList.getBillTime();

                list.add(sectionNote);

            }

            SectionNote  sectionNote = new SectionNote(false);

            EntityNote entityNote = new EntityNote();
            entityNote.setTitle(entityList.getPurpose());
            if(entityList.getInAndOutType() == 1){

                entityNote.setDesc("+"+entityList.getMoney());

            }else if(entityList.getInAndOutType() == 2){

                entityNote.setDesc("-"+entityList.getMoney());

            }
            sectionNote.setEntityNote(entityNote);

            list.add(sectionNote);
        }



        return  list;
    }

    // </editor-fold>


}
